<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>平台用户</title>
		<link rel="stylesheet" href="layui/css/layui.css" media="all">
	</head>

	<body>

		<div class="layuimini-container">
			<div class="layuimini-main">

				<fieldset class="table-search-fieldset">
					<legend>搜索信息</legend>
					<div style="margin: 10px 10px 10px 10px">

						<div class="layui-form-item" style="margin-top: 20px;" onsubmit="false">
							<label class="layui-form-label">用户名</label>
							<div class="layui-input-inline" style="width: 120px;">
								<input type="text" name="name" id="name" class="layui-input">
							</div>

							<button class="layui-btn layui-btn-primary" id="search" data-type="reload">
							<i class="layui-icon"></i> 搜 索
						</button>
						</div>

					</div>
				</fieldset>
				<table class="layui-hide" id="test" lay-filter="test"></table>

				<script type="text/html" id="switchTpl">

					<input type="checkbox" style="width:30px;" name="IsLockout" id="IsLockout" value="{{d.id}}" lay-skin="switch" lay-text="开启|禁用" lay-filter="sexDemo" {{ d.isLockout==0 ? 'checked' : '' }}>
				</script>

				<script type="textml" id="toolbarDemo">
					<div class="layui-btn-container">
						<button class="layui-btn layui-btn-sm" lay-event="addUser">添加<tton>
			</div>
		</script>
			<script type="textml" id="barDemo">
			<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-xs" lay-event="pad">重置密码</a>
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
<a class="layui-btn layui-btn-xs" lay-event="csk">添加角色</a>
	</script>

		</div>
	</div>
</body>
<script src="js/User.js" type="text/javascript" charset="utf-8"></script>
<script src="layui/layui.js" charset="utf-8"></script>
<script>

		layui.use(['table', 'form','util','layer','transfer'], function() {
			var table = layui.table;
			var $ = layui.jquery;
			var form = layui.form;
			 var $ = layui.$
              ,transfer = layui.transfer
             ,layer = layui.layer
              ,util = layui.util;
			table.render({
				elem : '#test',
				page:'true',
				toolbar : '#toolbarDemo',
				url :globalData.server+'user/getUserAll',
				method : 'POST',
			
				cols : [ [ {
					field : 'id',
					title : '编号',
					width : 100,
				}, {
					field : 'name',
					title : '用户',
					width : 100,
				}, {
					field : 'phone',
					title : '手机号',
					width : 100
					
				}, {
					field : 'create_time',
					title : '创建时间',
					width : 100,
				},{
					field : 'enable',
					title : '状态',
					width : 100,
				},{
					title : '操作',
					width : 300,
					toolbar : '#barDemo'
				} ] ],
				id : 'testReload'
			});
			
			 //监听开关操作
           /*   form.on('switch(sexDemo)', function(data) {

					var IsLockout = this.checked ? 1 : 0;

					var huoqu = this.value;
					var url =globalData.server+'statics/users/updateIs';
					$.post(url, {
						"id":parseInt(huoqu),
						"IsLockout":parseInt(IsLockout)
					}, function(res) {
						console.log(res);
					}, "json");
					

				});*/
				
				form.on('switch(sexDemo)', function(data) {

	var IsLockout = this.checked ? 0 : 1;

	var id = this.value;
	var url = globalData.server + 'statics/users/updateIs';
	$.post(url, {
		"id": parseInt(id),
		"IsLockout": parseInt(IsLockout)
	}, function(res) {
		console.log(res);
	}, "json");

});


//查询
			$('#search').on('click', function() {								
								var name = $("#name").val();
							
							
								table.reload('testReload', {//testReload,是上面渲染表格的id
									where : {//接口需要的两个请求参数
									'name' : name
                          		
									}
								});
							});
			

		
			
			table.on('toolbar(test)', function(obj) { 
				switch (obj.event) {
				case 'addUser':
					layer.open({
						type : 2,
						area : [ '500px', '600px' ],
						content : 'usersInfo_add.html',
						title : '添加',
						end : function() {
							window.location.reload();
						}
					});
					break;
				}
				;
			});
			table.on('tool(test)', function(obj) {
				if (obj.event === 'del') {
					layer.confirm('真的删除行么', function(index) {
						$.post(globalData.server+"user/deleteUser", {
							"id" : obj.data.id,
							method : '/deleteUser'
						}, function(data) {
							if (data.count > 0) {
								console.log('删除成功');
								window.location.reload();
							} else {
								alert('不能删除！');
								window.location.reload();
							}
						}, "json");
					});
				} else if (obj.event === 'edit') {
					console.log(obj.data);
					layer.open({
						type : 2,
						area : [ '500px', '400px' ],
						content : 'usersInfo_update.html',
						title : '编辑',
						success : function(layero, index) {
							var body = layer.getChildFrame('body', index);
							body.contents().find("#id").val(obj.data.id);
							body.contents().find("#password").val(obj.data.password);							
							body.contents().find("#phone").val(obj.data.phone);
							body.contents().find("#name").val(obj.data.name);
							body.contents().find("#enable").val(obj.data.enable);
							body.contents().find("#role_id").val(obj.data.roleId);
							body.contents().find("#create_time").val(obj.data.create_time);
						},
						end : function() { 
							window.location.reload();
						}
					});
				}else if (obj.event === 'pad') {
        layer.confirm('真的重置么', function(index) {
          console.log('执行重置');
          console.log(obj.data);
          $.post(globalData.server+"statics/users/updatepsd", {
            "id" : obj.data.id,
            method : 'updatepsd'
          }, function(data) {
            if (data.count > 0) {
              console.log('重置成功');
              window.location.reload();
            } else {
              alert('不能重置！');
              window.location.reload();
            }
          }, "json");
        });
      }else if (obj.event === 'csk') {
            		var index = layer.open({
					title : '给'+globalData.getUserName()+'设置角色',
					type : 2,
					shade : 0.2,
					maxmin : true,
					shadeClose : true,
					area : ['500px', '400px' ],
					content : 'userroles.html',
					success : function(layero, index) {
						var body = layer.getChildFrame('body', index);
							body.find("#id").val(obj.data.id);
					},
					end : function() {
						window.location.reload();
					}
				});
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
			});
		});
	</script>
</html>